using System;
using System.Collections;
using System.IO;
using System.Management.Automation;
using System.Net;
using System.Xml;
using System.Text;

namespace PoshHttp
{
   [Cmdlet(VerbsData.ConvertFrom, "Html", SupportsShouldProcess = true)]
   public class ConvertFromHtmlCommand : PSCmdlet
   {

      #region Parameters
      [Parameter(
          Mandatory = false,
          ValueFromPipeline = true,
          HelpMessage = "HTML Source")]
      public string Source
      {
         get { return _source; }
         set { _source = value; }
      }
      string _source;

      #endregion
      protected override void BeginProcessing()
      {
         base.BeginProcessing();
         if (null != _source && _source.Length > 0)
         {
            WriteObject(Utility.ConvertHtml(_source));
         }
      }

      StringBuilder doc = new StringBuilder();
      protected override void ProcessRecord()
      {
         if (!string.IsNullOrEmpty(_source))
         {
            doc.AppendLine(_source);
         }
         else doc.AppendLine();
      }

      protected override void EndProcessing()
      {
         WriteObject(Utility.ConvertHtml(doc.ToString()));

         base.EndProcessing();
      }


      private CookieContainer myCookies = new CookieContainer();

/// <summary>
/// Post as a web-form to do fake submissions
/// </summary>
/// <param name="uri"></param>
/// <param name="data"></param>
/// <returns></returns>


      /// <summary>
      /// Synchronously loads the HTML document from the specified file and returns it as an XmlDocument.
      /// </summary>
      /// <param name="filepath">The file path.</param>
      /// <returns>An XML representation of the document</returns>
      protected XmlDocument LoadFile(string filePath)
      {
         XmlDocument doc = new XmlDocument();

         using (StreamReader reader = File.OpenText(filePath))
         {
            doc = Utility.ConvertHtml(reader);
            reader.Close();
         }
         return doc;
      }

      
      /// <summary>
      /// Get the UserAgent string for HTTP requests
      /// </summary>
      /// <value>The UserAgent string with app version info.</value>

   }
}